#include "UserInterface.h"

/** 
 * UserInterface constructor
 * Get instances of Input and DX9Engine
 */
UserInterface::UserInterface(void)
{
	state = UI_MENU;
	resume = false;

	pInput = Input::getInstance();
	pEngine = DX9Engine::getInstance();
	for(int i=0; i<MAX_MOUSEBUT; ++i)
		mbdown[i] = false;
}

UserInterface::~UserInterface(void)
{
}

void UserInterface::initialize()
{
	window.initialize(L"images/window.png", V2DF(SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.5), 0.0f, 1.0f, true);
	border.initialize(L"images/border.png");
	// border thickness
	const int t = 8;
	// initialize buttons
	for(int i=0; i<MENU_BUTCNT; ++i)
	{
		// menu buttons
		menuButton[i].initialize(L"images/button.png", V2DF(SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.5-200+(i*100)),
			0.0f, 1.0f, true);

		// set border rect
		SetRect(&borderRect[i], menuButton[i].getRect().left, menuButton[i].getRect().top, 
			menuButton[i].getRect().right+t, menuButton[i].getRect().bottom+t);
	}
	for(int i=0; i<IGMU_BUTCNT; ++i)
	{
		// in game menu buttons
		gameButton[i].initialize(L"images/button.png", V2DF(SCREEN_WIDTH*0.5, SCREEN_HEIGHT*0.5-100+(i*100)),
			0.0f, 1.0f, true);

		// set border rect
		SetRect(&borderRect[i], gameButton[i].getRect().left, gameButton[i].getRect().top, 
			gameButton[i].getRect().right+t, gameButton[i].getRect().bottom+t);
	}
}

void UserInterface::update()
{
	switch(state)
	{
	case UI_MENU:
		for(int i=0; i<MENU_BUTCNT; ++i)
		{
			// check if buttons clicked
			if(menuButton[i].hovered() && pInput->mouseButtonDown(0))
			{
				if(!mbdown[i])
				{
					mbdown[i] = true;
					DbgPrintf("Button [%d] clicked\n", i);
				}
			}
			else
			{
				if(mbdown[i])
				{
					mbdown[i] = false;
					DbgPrintf("Button [%d] released\n", i);
				}
			}
		}
		// start game
		if(menuButton[0].hovered() && pInput->mouseButtonDown(0))
		{
			state = UI_INGAME;
			DbgPrintf("Enter game state\n");
		}
		// exit
		if(menuButton[4].hovered() && pInput->mouseButtonDown(0))
		{
			PostQuitMessage(0);
		}
		break;
	case UI_INGAME:
		for(int i=0; i<IGMU_BUTCNT; ++i)
		{
			// check if buttons clicked
			if(gameButton[i].hovered() && pInput->mouseButtonDown(0))
			{
				if(!mbdown[i])
				{
					mbdown[i] = true;
					DbgPrintf("Button [%d] clicked\n", i);
				}
			}
			else
			{
				if(mbdown[i])
				{
					mbdown[i] = false;
					DbgPrintf("Button [%d] released\n", i);
				}
			}
		}
		// resume
		if(gameButton[0].hovered() && pInput->mouseButtonDown(0))
		{
			resume = true;
		}
		// menu
		if(gameButton[1].hovered() && pInput->mouseButtonDown(0))
		{
			state = UI_MENU;
		}
		// exit
		if(gameButton[2].hovered() && pInput->mouseButtonDown(0))
		{
			PostQuitMessage(0);
		}
		break;
	case UI_NONE:
		break;
	}
}

void UserInterface::render()
{ 
	switch(state)
	{
	case UI_MENU:
		for(int i=0; i<MENU_BUTCNT; ++i)
		{
			if(menuButton[i].hovered())
				border.draw(borderRect[i], menuButton[i].getPos(), 0.0, 1.0f, true);
			menuButton[i].draw();
		}
		break;
	case UI_INGAME:
		//window.draw();
		for(int i=0; i<IGMU_BUTCNT; ++i)
		{
			if(gameButton[i].hovered())
				border.draw(borderRect[i], gameButton[i].getPos(), 0.0, 1.0f, true);
			gameButton[i].draw();
		}
		break;
	case UI_NONE:
		break;
	}
}

/** Write text on button
 * Must determine bounds beforehand
 */
#define WriteButtonText( text, index) {					\
	sprintf_s(buffer, text);							\
	pEngine->writeTextCenter(buffer, temp[index]);		\
}														\

void UserInterface::drawText()
{
	char buffer[100];
	RECT temp[NUM_BUTTONS];
	switch(state)
	{
	case UI_MENU:
		for(int i=0; i<MENU_BUTCNT; ++i)
		{
			temp[i].top = menuButton[i].getPos().y-(menuButton[i].getHeight()*0.5f);
			temp[i].bottom = menuButton[i].getPos().y+(menuButton[i].getHeight()*0.5f);
			temp[i].left = menuButton[i].getPos().x-(menuButton[i].getWidth()*0.5f);
			temp[i].right = menuButton[i].getPos().x+(menuButton[i].getWidth()*0.5f);
		}
		WriteButtonText("Play", 0);
		WriteButtonText("Options", 1);
		WriteButtonText("Help", 2);
		WriteButtonText("Credits", 3);
		WriteButtonText("Exit", 4);
		break;
	case UI_INGAME:
		for(int i=0; i<IGMU_BUTCNT; ++i)
		{
			temp[i].top = gameButton[i].getPos().y-(gameButton[i].getHeight()*0.5f);
			temp[i].bottom = gameButton[i].getPos().y+(gameButton[i].getHeight()*0.5f);
			temp[i].left = gameButton[i].getPos().x-(gameButton[i].getWidth()*0.5f);
			temp[i].right = gameButton[i].getPos().x+(gameButton[i].getWidth()*0.5f);
		}
		WriteButtonText("Resume", 0);
		WriteButtonText("Menu", 1);
		WriteButtonText("Exit", 2);
		break;
	case UI_NONE:
		break;
	}
}

void UserInterface::setState(UIState s)
{
	state = s;
}

int UserInterface::getState()
{
	switch(state)
	{
	case UI_MENU:	return 0; break;
	case UI_INGAME: return 1; break;
	case UI_NONE:	return 2; break;
	}
}

bool UserInterface::isResumed()
{
	if(resume)
	{
		resume = false;
		return true;
	}
	return false;
}

void UserInterface::release()
{

}